<template>
  <BasicModal @register="register" :title="title" @ok="handleSubmit">

    <BasicForm @register="registerForm" />
    <p v-if="btnKey == 'btnUpdateJzDate'" style="color: red; margin-left: 20px">
      提示：请填写截止时间批量修改
    </p>
    <p v-if="btnKey == 'btnUpdateHfDate'" style="color: red; margin-left: 20px">
      提示：请填写回复时间批量修改
    </p>
    <a v-if="btnKey == 'btnUpdateHxOver'" style="color: black; position:left:50%;top50%;">
      <div style="margin-top: 20px; font-size: 20px">
        <Icon
          icon="simple-line-icons:question"
          style="margin-right: 10px; color: #f39c12; font-size: 30px"
        />
        确认修改[已完成]状态？
      </div>
    </a>
    <p v-if="btnKey == 'btnUpdateYgOverDate'" style="color: red; margin-left: 20px">
      提示：请填写回复时间批量修改
    </p>
  </BasicModal>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'WorkCalendarModal',
  });
</script>
<script setup lang="ts">
  import { defineComponent, ref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { Icon } from '/@/components/Icon';
  import { btnUpdateYgOverDate, btnUpdateJzDate, btnUpdateHfDate } from '/@/api/m8/order/order';
  const emit = defineEmits(['success', 'register']);
  const { showMessage } = useMessage();
  let record = ref<any>();
  const [register, { closeModal, setModalProps }] = useModalInner(async (e) => {
    value.value = '';
    record.value = e;
    title.value = e.title;
    btnKey.value = e.btnKey;

    await resetFields();

    let inputFormSchemas: FormSchema[] = [];

    if (e.btnKey == 'btnUpdateJzDate') {
      inputFormSchemas = [
        {
          label: '截止日期',
          field: 'hxjzDate',
          component: 'DatePicker',
          componentProps: {
            format: 'YYYY-MM-DD HH',
            showTime: { format: 'HH' },
          },
          required: true,
        },
      ];
    }
    if (e.btnKey == 'btnUpdateHfDate') {
      inputFormSchemas = [
        {
          label: '回复日期',
          field: 'hxhfDate',
          component: 'DatePicker',
          componentProps: {
            format: 'YYYY-MM-DD HH',
            showTime: { format: 'HH' },
          },
          required: true,
        },
      ];
    }
    if (e.btnKey == 'btnUpdateYgOverDate') {
      inputFormSchemas = [
        {
          label: '预估交付',
          field: 'allOverDate',
          component: 'DatePicker',
          componentProps: {
            format: 'YYYY-MM-DD HH',
            showTime: { format: 'HH' },
          },
          required: true,
        },
      ];
    }
    setProps({
      labelWidth: 120,
      schemas: inputFormSchemas,
      baseColProps: { lg: 24, md: 24 },
    });
  });
  let title = ref<string>('');
  let btnKey = ref<string>('');
  let value = ref<string>('');

  // 保存数据
  async function handleSubmit() {
    try {
      const data = await validate();
      setModalProps({ confirmLoading: true });
      if (title.value == '截止日期') {
        const params: any = {
          selIds: record.value.Ids,
          ...data,
        };
        const res = await btnUpdateJzDate(params);
        showMessage(res.message);
      }
      if (title.value == '回复日期') {
        const params: any = {
          selIds: record.value.Ids,
          ...data,
        };
        const res = await btnUpdateHfDate(params);
        showMessage(res.message);
      }
      if (title.value == '预估交付') {
        const params: any = {
          selIds: record.value.Ids,
          ...data,
        };
        console.log(data);
        const res = await btnUpdateYgOverDate(params);
        showMessage(res.message);
      }
      setTimeout(closeModal);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage('您填写的信息有误，请根据提示修正。');
      }
      console.log('error', error);
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
  const [registerForm, { resetFields, setFieldsValue, validate, setProps }] = useForm();
</script>
<style scoped>
  .ant-picker {
    width: 80%;
  }
</style>
